package com.kaibo.roomdemo.activity

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.os.Build
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.transition.Scene
import android.view.View
import android.view.ViewAnimationUtils
import android.view.animation.AccelerateInterpolator
import com.kaibo.roomdemo.R
import kotlinx.android.synthetic.main.activity_animation_demo.*

class AnimationDemoActivity : AppCompatActivity() {

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_animation_demo)

//        window.decorView.animate().apply {
//            duration=2000
//            scaleX(1f)
//        }.start()

        val sceneForLayout = Scene.getSceneForLayout(scene_container1, R.layout.animation_scene_layout, this)

//        sceneForLayout.setEnterAction {
//
//        }

        switch_view.setOnCheckedChangeListener { _, isChecked ->

            scene_container1.clearAnimation()

            if (isChecked) {

                val centerX = scene_container1.width / 2
                val centerY = scene_container1.height / 2

                val radius = Math.sqrt((centerX * centerX + centerY * centerY).toDouble()).toFloat()

                val createCircularReveal = ViewAnimationUtils.createCircularReveal(scene_container1, centerX, centerY, 0f, radius)

                createCircularReveal
                        .apply {
                            duration = 2000
                            interpolator = AccelerateInterpolator()
                            addListener(object : AnimatorListenerAdapter() {
                                override fun onAnimationStart(animation: Animator?) {
                                    scene_container1.visibility = View.VISIBLE
                                }
                            })
                        }
                        .start()



//                val inflateTransition = TransitionInflater.from(this).inflateTransition(R.transition.slide_and_changebounds_sequential_with_interpolators)


//                screen_container1
//                        .animate()
//                        .apply {
//                            duration = 500
//                            scaleY(1f)
//                        }
//                        .start()
            } else {
//                screen_container1
//                        .animate()
//                        .apply {
//                            duration = 500
//                            scaleY(0f)
//                        }
//                        .start()

                val centerX = scene_container1.width / 2
                val centerY = scene_container1.height / 2

                val radius = Math.sqrt((centerX * centerX + centerY * centerY).toDouble()).toFloat()

                val createCircularReveal = ViewAnimationUtils.createCircularReveal(scene_container1, centerX, centerY, radius, 0f)

                createCircularReveal.apply {
                    duration = 2000
                    interpolator = AccelerateInterpolator()
                    addListener(object : AnimatorListenerAdapter() {
                        override fun onAnimationEnd(animation: Animator?) {
                            scene_container1.visibility = View.INVISIBLE
                        }
                    })
                }.start()

            }
        }

    }
}
